package com.feeyin.radar.crm.controller;

import com.feeyin.radar.commons.Page;
import com.feeyin.radar.commons.StringUtils;
import com.feeyin.radar.crm.service.CustomerService;
import com.feeyin.radar.crm.service.RepeatCustomerService;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.springframework.web.servlet.view.RedirectView;

public class RepeatCustomerController extends MultiActionController
{
  private RepeatCustomerService repeatCustomerService;
  private CustomerService customerService;
  private String repeatCustomer;
  private String repeatCustomerInfo;

  public RepeatCustomerService getRepeatCustomerService()
  {
    return this.repeatCustomerService;
  }

  public void setRepeatCustomerService(RepeatCustomerService repeatCustomerService) {
    this.repeatCustomerService = repeatCustomerService;
  }

  public CustomerService getCustomerService() {
    return this.customerService;
  }

  public void setCustomerService(CustomerService customerService) {
    this.customerService = customerService;
  }

  public String getRepeatCustomer() {
    return this.repeatCustomer;
  }

  public void setRepeatCustomer(String repeatCustomer) {
    this.repeatCustomer = repeatCustomer;
  }

  public String getRepeatCustomerInfo() {
    return this.repeatCustomerInfo;
  }

  public void setRepeatCustomerInfo(String repeatCustomerInfo) {
    this.repeatCustomerInfo = repeatCustomerInfo;
  }

  public ModelAndView display(HttpServletRequest request, HttpServletResponse response)
  {
    Map map = new HashMap();
    map.put("result", request.getParameter("result"));
    map.put("deleteResult", request.getParameter("deleteResult"));

    if (this.repeatCustomerService.isRepeatSet()) {
      Integer userId = (Integer)request.getSession().getAttribute("userId");
      String sFlag = request.getParameter("sFlag");
      String customerName = "";
      if ((!StringUtils.isEmpty(sFlag)) && ("1".equals(sFlag))) {
        customerName = request.getParameter("customerName");
      }
      map.put("sFlag", sFlag);
      map.put("customerName", customerName);

      Page page = new Page();
      page.getPageList(this.repeatCustomerService.getPageRepeatCustomersCount(userId, customerName).intValue(), Integer.valueOf(5), request);
      map.put("customers", this.repeatCustomerService.getPageRepeatCustomers(userId, customerName, page));
    }

    return new ModelAndView(getRepeatCustomer(), map);
  }

  public ModelAndView repeatCustomerInfo(HttpServletRequest request, HttpServletResponse response)
  {
    Map map = new HashMap();

    Integer userId = (Integer)request.getSession().getAttribute("userId");
    String id = request.getParameter("id");
    map.put("selectCustomerId", id);
    if (!StringUtils.isEmpty(id)) {
      map.put("customers", this.repeatCustomerService.getRepeatCustomerInfos(userId, Integer.valueOf(Integer.parseInt(id))));
    }
    return new ModelAndView(getRepeatCustomerInfo(), map);
  }

  public ModelAndView mergeCustomer(HttpServletRequest request, HttpServletResponse response)
  {
    boolean result = false;
    int success = 0;

    Integer userId = (Integer)request.getSession().getAttribute("userId");
    String selectCustomerId = request.getParameter("selectCustomerId");
    String mainCustomerId = request.getParameter("mainCustomerId");
    String salePersonId = request.getParameter("salePersonId");
    Integer personId = null;
    if (!StringUtils.isEmpty(salePersonId)) {
      personId = Integer.valueOf(Integer.parseInt(salePersonId));
    }
    if ((!StringUtils.isEmpty(selectCustomerId)) && 
      (!StringUtils.isEmpty(mainCustomerId))) {
      result = this.repeatCustomerService.mergeCustomer(userId, Integer.valueOf(Integer.parseInt(selectCustomerId)), Integer.valueOf(Integer.parseInt(mainCustomerId)), personId);
      if (result) success = 1;
    }

    return new ModelAndView(new RedirectView("./repeatCustomer.do?action=display&result=" + success));
  }
}